/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.manage.core.repository.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.component.data.repository.beetlsql.service.AbstractBeetlSqlRepository;
import info.peigen.hotpot.manage.core.domain.Resource;
import info.peigen.hotpot.manage.core.repository.entity.ResourceEntity;
import info.peigen.hotpot.manage.core.repository.mapper.ResourceMapper;
import info.peigen.hotpot.manage.core.util.convertor.ResourceConvertor;
import org.noear.solon.annotation.Component;

import java.util.List;

/**
 * <b>(ResourceRepository)</b>
 * "资源" 仓储服务
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2024-06-28
 */
@SuppressWarnings("unused")
@Component
public class ResourceRepository extends AbstractBeetlSqlRepository<ResourceMapper, ResourceEntity> {
    public Resource active(String resourceId) {
        Assert.isTrue(StrUtil.isNotEmpty(resourceId), () -> new BusinessException(ResultCode.FAILURE, "激活时[resourceId]不得为空!"));
        ResourceEntity entity = getMapper().findByResourceId(resourceId);
        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "激活失败!resourceId=" + resourceId));

        return ResourceConvertor.INSTANCE.convertToDomain(entity);
    }

    public List<Resource> active(List<String> resourceIds) {
        Assert.isTrue(CollUtil.isNotEmpty(resourceIds), () -> new BusinessException(ResultCode.FAILURE, "激活时[resourceIds]不得为空!"));
        List<ResourceEntity> entities = getMapper().listByResourceIds(resourceIds);
        Assert.notNull(entities, () -> new BusinessException(ResultCode.FAILURE, "激活失败!resourceId=" + resourceIds));

        return ResourceConvertor.INSTANCE.convertToDomainsByEntity(entities);

    }

    public Resource lock(String resourceId) {
        Assert.isTrue(StrUtil.isNotEmpty(resourceId), () -> new BusinessException(ResultCode.FAILURE, "加锁时[resourceId]不得为空!"));
        ResourceEntity entity = getMapper().lockByResourceId(resourceId);
        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "锁定失败!resourceId为空!"));
        return ResourceConvertor.INSTANCE.convertToDomain(entity);
    }

    public void removeByResourceId(String resourceId) {
        Assert.isTrue(StrUtil.isNotEmpty(resourceId), () -> new BusinessException(ResultCode.FAILURE, "删除时[resourceId]不得为空!"));
        getMapper().deleteByResourceId(resourceId);
    }

    public void logicDeleteById(Integer id){
        delete(id);
    }

    public int logicDeleteByResourceId(String resourceId) {
        Assert.isTrue(StrUtil.isNotEmpty(resourceId), () -> new BusinessException(ResultCode.FAILURE, "逻辑删除时[resourceId]不得为空!"));
        return getMapper().createLambdaQuery().andEq(ResourceEntity::getResourceId, resourceId).delete();
    }

    public void store(Resource domain){
        Assert.isTrue(ObjectUtil.isNotEmpty(domain), () -> new BusinessException(ResultCode.FAILURE, "持久化时[domain]不得为空!"));
        ResourceEntity entity = ResourceConvertor.INSTANCE.convertToEntity(domain);
        saveOrUpdate(entity);
    }

}